/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import com.cea.papyrus.diagram.sequence.sedi.edit.FrameEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.analysis.CoverableAnalyser;
import com.cea.papyrus.diagram.sequence.sedi.model.command.LifelinePosition;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Performs geometrical analysis of a diagram in order to detect lifeline
 * covered by interaction fragments.
 * <p>
 * The following attributes must be set before the command can run successfully:
 * </p>
 * <ul>
 * <li>interactionEditPart     - the interaction edit part
 * </ul>
 * <p>
 * The following attributes may be set:
 * <ul>
 * <li>fragmentNode	- the covering fragment
 * <li>bounds		- the area where the lifeline is supposed to be
 * <li> Either fragmentNode or bounds must be set, if bounds is set, 
 * these bounds will be used in place for the fragmentView.bounds.
 * </ul>
 * <p>
 * When completed, the following attributes can be read to access the result:
 * </p>
 * <ul>
 * <li>Covered
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class AnalyseBestCoveredCommand extends Command {

	private FrameEditPart interactionEditPart;
	private GraphNode fragmentNode;
	private Rectangle bounds;
	private List<LifelinePosition> covered;

	public void setFragmentNode(final GraphNode fragmentNode) {
		this.fragmentNode = fragmentNode;
	}

	public void setInteractionEditPart(final FrameEditPart interactionEditPart) {
		this.interactionEditPart = interactionEditPart;
	}

	public void setBounds(final Rectangle bounds) {
		this.bounds = bounds;
	}

	public List<LifelinePosition> getCovered() {
		return covered;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {

		GraphNode node = interactionEditPart.getGraphNode();
		final CoverableAnalyser analyser = 
			new CoverableAnalyser(interactionEditPart, node);
		
		covered = analyser.findBestCovered(fragmentNode, bounds);
	}

	@Override
	public void undo() {
		covered = null;
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void dispose() {
		fragmentNode = null;
		covered = null;
		interactionEditPart = null;
	}
}
